package com.hzya.frame.hr.company.controller; 
import com.hzya.frame.hr.staff.service.IStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.hzya.frame.utils.core.action.DefaultController;
import com.hzya.frame.utils.core.entity.JsonResultEntity;
import com.hzya.frame.utils.core.page.PageInfo;
import com.hzya.frame.hr.company.entity.CompanyEntity;
import com.hzya.frame.hr.company.service.ICompanyService;

import java.util.List;

/**
 * @description: 企业管理  controller
 * @tableName: hr_company 
 * @entityName: CompanyEntity
 * 包含：
 * 1、分页查询(queryPaged)
 * 2、index页面(goIndexPage)
 * 3、新增页面(goAddPage)
 * 4、新增信息保存(save)
 * 5、修改页面(goEditPage)
 * 6、修改信息保存(update)
 * 7、信息查看(goShowPage)
 * 8、删除信息(delete)
 * @author: gjh
 * @history: 1.0
 */

@Controller("hr_company")
@RequestMapping("/hr/company/CompanyController")
public class CompanyController extends DefaultController {
    @Autowired
    protected ICompanyService  companyService;
	/** 员工service*/
	private IStaffService staffService;
    /** 企业管理分页信息 **/
    @RequestMapping(value = "queryPaged")
    @ResponseBody
    public PageInfo<CompanyEntity> queryPaged( CompanyEntity entity) throws Exception {
		PageInfo<CompanyEntity> page =null;
		try {
		   page = companyService.queryPaged(entity);
		} catch (Exception e) {
		    e.printStackTrace();
		}
		return page;
    }

	/** 跳转到企业管理的index页面 **/
    @RequestMapping(value = "goIndexPage", method = RequestMethod.GET)
    public ModelAndView goIndexPage() {
		ModelAndView view = new ModelAndView();
		/**这里根据实际的需要填写需要传到前台的对象**/
		//view.addObject(attributeName, attributeValue);
		/**这里是返回对应的页面路径**/
		view.setViewName("/hr/company/company/list");
		return view;
    }

	/** 跳转到企业管理信息新增页面 **/
    @RequestMapping(value = "goAddPage")
    public ModelAndView goAddPage() {
		ModelAndView view = new ModelAndView();
		/**这里根据实际的需要填写需要传到前台的对象**/
		//view.addObject(attributeName, attributeValue);
		/**这里是返回对应的页面路径**/
		CompanyEntity companyEntity = new CompanyEntity();
		/** 把当前用户所在的公司，和子公司查询出来*/
		List<CompanyEntity> companyEntityList = companyService.listGetCompanyEntityToUser(companyEntity);
		view.addObject("companyEntityList",companyEntityList);
		view.setViewName("hr/company/company/add");
		return view;
    }
    
    /** 企业管理新增信息保存 **/
    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity save( CompanyEntity entity) {
		try {
		    companyService.save(entity);
		    return getSuccessMessageEntity("保存成功", "save");
		} catch (Exception e) {
		    logger.error("save", e);
		    e.printStackTrace();
		    return getFailureMessageEntity("保存失败！"+e.getMessage(), "save");
		}
    }
    
    /** 跳转到企业管理信息修改页面 **/
    @RequestMapping(value = "edit/{id}")
    public ModelAndView goEditPage(@PathVariable("id") Long id) {
		ModelAndView view = new ModelAndView();
		/**这里根据实际的需要填写需要传到前台的对象**/
		CompanyEntity entity = companyService.get(id);
		view.addObject("entity", entity);
		/** 把当前用户所在的公司，和子公司查询出来*/
		CompanyEntity companyEntity = new CompanyEntity();
		List<CompanyEntity> companyEntityList = companyService.listGetCompanyEntityToUser(companyEntity);
		view.addObject("companyEntityList",companyEntityList);
		/**这里是返回对应的页面路径**/
		view.setViewName("/hr/company/company/edit");
		return view;
    }
    
    /** 企业管理修改信息保存 **/
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity update( CompanyEntity entity) {
		try {
		    companyService.update(entity);
		    return getSuccessMessageEntity("更新成功！", "update");
		} catch (Exception e) {
		    logger.error("update", e);
		    e.printStackTrace();
		    return getFailureMessageEntity("更新失败！"+e.getMessage(), "update");
		}
    }
    
    /** 跳转到企业管理信息查看 **/
    @RequestMapping(value = "show/{id}")
    public ModelAndView goShowPage(@PathVariable("id") Long id) {
		ModelAndView view = new ModelAndView();
		/**这里根据实际的需要填写需要传到前台的对象**/
		CompanyEntity entity = companyService.get(id);
		view.addObject("entity", entity);
		/** 把当前用户所在的公司，和子公司查询出来*/
		CompanyEntity companyEntity = new CompanyEntity();
		List<CompanyEntity> companyEntityList = companyService.listGetCompanyEntityToUser(companyEntity);
		view.addObject("companyEntityList",companyEntityList);
		/**这里是返回对应的页面路径**/
		view.setViewName("/hr/company/company/show");
		return view;
    }

	/** 删除企业管理信息 **/
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity delete(Long id) {
		try {
		    companyService.logicRemove(id);
		    return getSuccessMessageEntity("删除成功！", "delete");
		} catch (Exception e) {
		    logger.error("delete", e);
		    e.printStackTrace();
		    return getFailureMessageEntity("删除失败！"+e.getMessage(), "delete");
		}
    }

    @RequestMapping(value = "listGetCompanyEntity")
	@ResponseBody
	public List<CompanyEntity> listGetCompanyEntity(CompanyEntity entity){

    	return  companyService.query(entity);
	}

}

